-- UIConfirm3
-- Create by huanghr
-- 确认提示框3

CONFIRM_TYPE_OK          = 1
CONFIRM_TYPE_OK_CANCEL   = 2

CONFIRM_BG_TYPE_PET       = 1
CONFIRM_BG_TYPE_WORKSHOP  = 2
CONFIRM_BG_TYPE_FOOD      = 3

-- UIConfirm3继承自Layer
UIConfirm3 = class("UIConfirm3", function()
    return cc.Layer:create();
end)

----------------------------------------------------
-- @param confirmType  类型 CONFIRM_TYPE_OK 只有确认按钮， CONFIRM_TYPE_OK_CANCEL 有确认取消按钮
-- @param title        确定标题文本
-- @param msg          提示文本内容（为table, 暂分为三段文本）
-- @param iconPath     图片路径
-- @param okFunc       点击确认的处理函数
-- @param cancelFunc   点击取消的处理函数
-- @param okText       确定按钮文本
-- @param cancelText   取消按钮文本
-- @param extraPara    附加参数
function UIConfirm3.create(confirmType, title, msg, iconPath,  okFunc, cancelFunc, okText, cancelText, extraPara)
    return UIConfirm3.new(confirmType, title, msg, iconPath, okFunc, cancelFunc, okText, cancelText, extraPara);
end

-- 内部函数声明
local resize;

-- 构造函数
function UIConfirm3:ctor(confirmType, title, msg, iconPath, okFunc, cancelFunc, okText, cancelText, extraPara)
    self:setName("UIConfirm3");
    -- 初始化
    local node = cc.CSLoader:createNode("layout/dialog/Confirm3.csb");
    self:addChild(node);
    self.node = node;
    self.type = confirmType;

    local CT = findChildByName(node, "CT");

    -- 附加参数
    extraPara = extraPara or {};

    -- 设置标题
    self.titleLabel = CT:getChildByName("title");
    TextStyleM.setTitleStyle(self.titleLabel);
    self.titleLabel:setString(title);

    -- 设置文本1的内容
    local contentLabelOne = CT:getChildByName("content_1");
    TextStyleM.setTextStyle(contentLabelOne, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    contentLabelOne:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_SIZE_SMALL + 5);

    contentLabelOne = applyString(contentLabelOne, msg[1]);
    self.contentLabelOne = contentLabelOne;

    -- 设置文本2的内容
    local contentLabelTwo = findChildByName(CT, "content_node/content_2");
    contentLabelTwo:setFontSize(TextStyleM.TEXT_SIZE_SMALL);
    contentLabelTwo:setTextColor(TextStyleM.TEXT_COLOR_BLUE);
    contentLabelTwo:setString(msg[4]);
    self.contentLabelTwo = contentLabelTwo;

    -- 设置昵称
    local nicknameLabel = findChildByName(CT, "content_node/nickname_label");
    TextStyleM.setTextStyle(nicknameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    nicknameLabel:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_SIZE_SMALL + 5);
    nicknameLabel = applyString(nicknameLabel, msg[2]);
    -- nicknameLabel:setString(msg[4]);
    self.nicknameLabel = nicknameLabel;

    -- 设置文本3的内容
    local contentLabelThree = findChildByName(CT, "content_node2/content_3");
    contentLabelThree:setFontSize(TextStyleM.TEXT_SIZE_SMALL);
    contentLabelThree:setTextColor(TextStyleM.TEXT_COLOR_BLUE);
    contentLabelThree:setString(msg[5]);
    self.contentLabelThree = contentLabelThree;

    local nationLabel = findChildByName(CT, "content_node2/nation_label");
    TextStyleM.setTextStyle(nationLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    nationLabel:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_SIZE_SMALL + 5);
    nationLabel = applyString(nationLabel, msg[3]);
    -- nationLabel:setString(msg[5]);
    self.nationLabel = nationLabel;

    -- 加载图片
    local contentImg = findChildByName(CT, "content_node/content_img");
    contentImg:loadTexture(iconPath[1]);
    self.contentImg = contentImg;

    -- 加载图片2
    local contentImg2 = findChildByName(CT, "content_node2/content_img_bg/content_img2");
    contentImg2:loadTexture(iconPath[2]);
    self.contentImg2 = contentImg2;

    if self.type == CONFIRM_TYPE_OK then
        self.okBtn = findChildByName(CT, "ok/btn_ok");
        self.okBtn:setVisible(true);
    elseif self.type == CONFIRM_TYPE_OK_CANCEL then
        self.okBtn = findChildByName(CT, "ok_cancel/btn_ok");
        self.okBtn:setVisible(true);

        self.cancelBtn = findChildByName(CT, "ok_cancel/btn_cancel");
        self.cancelBtn:setVisible(true);
    end

    -- 注册确认按钮点击事件
    local okBtn = self.okBtn;
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            UIMgr.getCurrentScene():removeDialog(self);
            if type(okFunc) == 'function' then
                okFunc();
                return true;
            end
            return false;
        end
    end
    okBtn:addTouchEventListener(onOkClick);

    -- 设置确定按钮文本
    TextStyleM.setTextStyle(okBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    if not okText then
        okBtn:setTitleText(getLocStr("confirm"));
    else
        okBtn:setTitleText(okText);
    end
    self.onOkClick = onOkClick;

    -- 注册取消按钮点击事件
    local cancelBtn = self.cancelBtn;
    if cancelBtn ~= nil then
        local function onCancelClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_return");
                UIMgr.getCurrentScene():removeDialog(self);
                if type(cancelFunc) == 'function' then
                    cancelFunc();
                end
            end
        end
        AddBackEventHandler(self, cancelBtn, onCancelClick);
        TextStyleM.setTextStyle(cancelBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        cancelBtn:setTitleText(getLocStr("btn_text_cancel"));

        self.onCancelClick = onCancelClick;

        -- 设置取消按钮文本
        if not cancelText then
            cancelBtn:setTitleText(getLocStr("btn_text_cancel"));
        else
            cancelBtn:setTitleText(cancelText);

            -- 如果自定义取消按钮文本，则按钮的样式和确定按钮一致
            cancelBtn:loadTextures("images/ui/button/btn_large_yellow1.png", "images/ui/button/btn_large_yellow2.png");
        end

        -- 如果有传入按钮的颜色，则设置按钮样式
        local okColor     = extraPara.okColor;
        local cancelColor = extraPara.cancelColor;
        if okColor ~= nil then
            okBtn:loadTextures(string.format("images/ui/button/btn_large_%s1.png", okColor), string.format("images/ui/button/btn_large_%s2.png", okColor));
        end

        if cancelColor ~= nil then
            cancelBtn:loadTextures(string.format("images/ui/button/btn_large_%s1.png", cancelColor), string.format("images/ui/button/btn_large_%s2.png", cancelColor));
        end
    end

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    btnBack:setTitleText(getLocStr("btn_text_back"));
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeDialog(self);

            if type(extraPara.backFunc) == "function" then
                -- 如果传入了返回回调，调用之
                extraPara.backFunc();
            end
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 调整高度
    self:adjustHeight();

    -- 适配
    resize(node);
end

-- 设置底图类型
function UIConfirm3:setBgType(bgType)
    local bg1 = findChildByName(self.node, "CT/bg1")

    if bgType == CONFIRM_BG_TYPE_PET then
        bg1:loadTexture("images/ui/common_bg/pet.png");
    elseif bgType == CONFIRM_BG_TYPE_WORKSHOP then
        bg1:loadTexture("images/ui/common_bg/pet.png");

        local bg2 = findChildByName(self.node, "CT/bg2");
        bg2:setVisible(false);
    elseif bgType == CONFIRM_BG_TYPE_FOOD then
        bg1:loadTexture("images/ui/common_bg/food.png");
    end
end

-- 调整高度
function UIConfirm3:adjustHeight()
    -- 老的高度和缩放
    local bgFrame = findChildByName(self.node, "CT/bg_frame");
    local oldHeight = 272 * bgFrame:getScaleY();
    local oldScaleY = bgFrame:getScaleY();

    local textHeight1 = self.contentLabelOne:getContentSize().height * 1.2;
    local textHeight2 = self.contentImg:getContentSize().height * 1.1;
    local textHeight3 = self.contentImg2:getContentSize().height * 1.1;

    local textHeight = textHeight1 + textHeight2 + textHeight3;

    local margin = 160;
    local newHeight = textHeight + margin * 2;

    if newHeight <= oldHeight then
        return;
    end

    -- 新的Y轴缩放
    local newScaleY = newHeight * oldScaleY / oldHeight;
    bgFrame:setScaleY(newScaleY);

    local delta = (newHeight - oldHeight) / 2;

    -- 标题上移delta像素
    self.titleLabel:setPositionY(self.titleLabel:getPositionY() + delta);

    -- 底图
    local bg1 = findChildByName(self.node, "CT/bg1");
    local bg2 = findChildByName(self.node, "CT/bg2");
    bg1:setPositionY(bg1:getPositionY() + delta);
    bg2:setPositionY(bg2:getPositionY() + delta);

    -- 按钮下移delta像素
    self.okBtn:setPositionY(self.okBtn:getPositionY() - delta);
    if self.cancelBtn then
        self.cancelBtn:setPositionY(self.cancelBtn:getPositionY() - delta);
    end

    -- 适配文本的位置
    self.contentLabelOne:setPositionY(self.titleLabel:getPositionY() - 80);

    -- 图片和文本的间距
    local imagGap = 10;

    -- 调整content_2的坐标的位置
    local label2Width = self.contentLabelTwo:getContentSize().width;
    local nicknameWidth = self.nicknameLabel:getContentSize().width;
    local imgWidth = self.contentImg:getContentSize().width;

    self.nicknameLabel:setPositionX(self.contentImg:getPositionX() + imgWidth + imagGap);
    self.contentLabelTwo:setPositionX(self.nicknameLabel:getPositionX() + nicknameWidth + imagGap);

    local contentNode = findChildByName(self.node, "CT/content_node");
    contentNode:setPositionX(-(imgWidth + nicknameWidth + label2Width + imagGap*2) / 2);

    -- 设置content_node2的位置
    local label3Width = self.contentLabelThree:getContentSize().width;
    local nationWidth = self.nationLabel:getContentSize().width;
    local img2Width = self.contentImg2:getContentSize().width;

    self.nationLabel:setPositionX(self.contentImg2:getPositionX() + img2Width + imagGap);
    self.contentLabelThree:setPositionX(self.nationLabel:getPositionX() + nationWidth + imagGap);
    local contentNode2 = findChildByName(self.node, "CT/content_node2");
    contentNode2:setPositionX(-(img2Width + nationWidth + label3Width + imagGap*2) / 2);
end

-- 适配
resize = function(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

return UIConfirm3;
